﻿<%@ WebHandler Language="C#" Class="list" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.OracleClient;
using System.Net.Mail;
using System.Net;
using System.Web.Script.Serialization;
public class list : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    private StringBuilder sb = null;
    public DataSet ds = null;
    public DataTable dt = null;
    private string sqlwhere = "";
    public void ProcessRequest(HttpContext context)
    {
        string type = PageBase.ext_string(context.Request.Params["type"]);
        switch (type)
        {
            //获取快递公司列表
            case "getkdgs":
                getkdgs(context);
                break;
            //获取发货申请列表
            case "get_deliver_list":
                get_deliver_list(context);
                break;
            //发货信息提交
            case "save_fhinfo":
                save_fhinfo(context);
                break;
            //获取返回消息
            case "get_reinfo":
                get_reinfo(context);
                break;
            //拒单重新发布
            case "againfabu":
                againfabu(context);
                break;
            //获取申请货品详情
            case "get_product_list":
                get_product_list(context);
                break;
            default:
                break;
        }
    }
    //拒单重新发布
    public void againfabu(HttpContext context)
    {
        string getids = PageBase.ext_string(context.Request.Params["getids"]);
        string msg = "";
        bool flag;
        string sqldelete = "exec [zdy_重新发布抢单] {0}";
        sqldelete = string.Format(sqldelete, getids);
        DataTable dtdelete= DBHelper.ExecuteDataTable(sqldelete);
        if (dtdelete.Rows.Count > 0)
        {
            flag = true;
            msg = dtdelete.Rows[0]["remsg"].ToString();
        }
        else
        {
            flag = false;
            msg = dtdelete.Rows[0]["remsg"].ToString();
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //获取申请货品详情
    public void get_product_list(HttpContext context)
    {
        string docnos = PageBase.ext_string(context.Request.Params["docnos"]);
        string sqllist = @"select a.docno,b.name,b.yscode+b.ysname ys,b.cmwm,b.value,b.nf,b.jj,b.price,
 'http://g.satchi.com.cn'+replace(imageurl,'Attach','Attach2') imgurl,a.sl FROM
  t_transferDeliver_mx a left join t_product b on a.tmid=b.id
 where a.docno='{0}'";
        sqllist = string.Format(sqllist,docnos);
        DataTable dtResult = DBHelper.ExecuteDataTable(sqllist);
        sb = new StringBuilder();
        sb.Append("{\"item\":[");
        for (int i = 0; i < dtResult.Rows.Count; i++)
        {
            sb.Append("{\"docno\":\"" + dtResult.Rows[i]["docno"].ToString() + "\",");
            sb.Append("\"name\":\"" + dtResult.Rows[i]["name"].ToString() + "\",");
            sb.Append("\"ys\":\"" + dtResult.Rows[i]["ys"].ToString() + "\",");
            sb.Append("\"cmwm\":\"" + dtResult.Rows[i]["cmwm"].ToString() + "\",");
            sb.Append("\"value\":\"" + dtResult.Rows[i]["value"].ToString() + "\",");
            sb.Append("\"nf\":\"" + dtResult.Rows[i]["nf"].ToString() + "\",");
            sb.Append("\"jj\":\"" + dtResult.Rows[i]["jj"].ToString() + "\",");
            sb.Append("\"price\":\"" + dtResult.Rows[i]["price"].ToString() + "\",");
            sb.Append("\"imgurl\":\"" + dtResult.Rows[i]["imgurl"].ToString() + "\",");
            sb.Append("\"sl\":\"" + dtResult.Rows[i]["sl"].ToString() + "\"}");
            if (i < dtResult.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //获取返回消息
    public void get_reinfo(HttpContext context)
    {
        string msg = "";
        bool flag=false;
        string docno = PageBase.ext_string(context.Request.Params["docnos"]);
        string sqllist = @"select bjmsg from t_transferDeliver_main where docno='{0}' and ltrim(rtrim(isactive))='Y'";
        sqllist = string.Format(sqllist,docno);
        DataTable dtResult = DBHelper.ExecuteDataTable(sqllist);
        if (dtResult.Rows.Count > 0)
        {
            flag = true;
            msg = dtResult.Rows[0]["bjmsg"].ToString();
        }
        else
        {
            flag = false;
            msg = "查询失败，请联系管理员";
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //提交发货信息
    public void save_fhinfo(HttpContext context)
    {
        string msg = "";
        string sqlsave = "";
        bool flag=false;
        GetDate list = new GetDate();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
        {
            string s = sr.ReadToEnd();
            list = new JavaScriptSerializer().Deserialize<GetDate>(s);
        }
        if (list.ArrayObj_main.Count > 0)
        {
            string docno = Utility.Trim(list.ArrayObj_main[0].DOCNO);
            string txt_kdgs = Utility.Trim(list.ArrayObj_main[0].TXT_KDGS);
            string txt_kddh = Utility.Trim(list.ArrayObj_main[0].TXT_KDDH);
            string txt_kdfhr = Utility.Trim(list.ArrayObj_main[0].TXT_KDFHR);
            string txt_kdtel = Utility.Trim(list.ArrayObj_main[0].TXT_KDTEL);
            string sqlfb = "exec [zdy_调拨发货] '{0}','{1}','{2}','{3}','{4}'";
            sqlfb = string.Format(sqlfb, docno,txt_kdgs,txt_kddh,txt_kdfhr,txt_kdtel);
            DataTable dtfb = DBHelper.ExecuteDataTable(sqlfb);
            if (dtfb.Rows.Count > 0)
            {
                if (dtfb.Rows[0]["zt"].ToString() == "成功")
                {
                    flag = true;
                    msg = dtfb.Rows[0]["remsg"].ToString();
                }
                else
                {
                    flag = false;
                    msg = dtfb.Rows[0]["remsg"].ToString();
                }
            }
            else
            {
                flag = false;
                msg = "失败，请联系管理员！";
            }
        }
        else
        {
            flag = false;
            msg = "失败，请联系管理员！";
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //获取发货列表
    public void get_deliver_list(HttpContext context)
    {
        string customerCode = PageBase.ext_string(context.Request.Params["customerCodes"]);
        string mobile= PageBase.ext_string(context.Request.Params["mobiles"]);
        string lxs= PageBase.ext_string(context.Request.Params["lxs"]);
        string sqllist = string.Format("exec [dbo].[zdy_查询调拨发货] '{0}','{1}'",lxs,mobile);
        DataTable dtResult = DBHelper.ExecuteDataTable(sqllist);
        sb = new StringBuilder();
        sb.Append("{\"item\":[");
        for (int i = 0; i < dtResult.Rows.Count; i++)
        {
            sb.Append("{\"docno\":\"" + dtResult.Rows[i]["docno"].ToString() + "\",");
            sb.Append("\"requestdocno\":\"" + dtResult.Rows[i]["requestdocno"].ToString() + "\",");
            sb.Append("\"id\":\"" + dtResult.Rows[i]["id"].ToString() + "\",");
            sb.Append("\"requestid\":\"" + dtResult.Rows[i]["requestid"].ToString() + "\",");
            sb.Append("\"docno\":\"" + dtResult.Rows[i]["docno"].ToString() + "\",");
            sb.Append("\"takeman\":\"" + dtResult.Rows[i]["buy_takeman"].ToString() + "\",");
            sb.Append("\"takephone\":\"" + dtResult.Rows[i]["buy_taketel"].ToString() + "\",");
            sb.Append("\"takeaddress\":\"" + dtResult.Rows[i]["buy_takeaddress"].ToString() + "\",");
            sb.Append("\"buyusername\":\"" + dtResult.Rows[i]["buyusername"].ToString() + "\",");
            sb.Append("\"saleusername\":\"" + dtResult.Rows[i]["saleusername"].ToString() + "\",");
            sb.Append("\"sale_kdgs\":\"" + dtResult.Rows[i]["sale_kdgs"].ToString() + "\",");
            sb.Append("\"sale_kddjbh\":\"" + dtResult.Rows[i]["sale_kddjbh"].ToString() + "\",");
            sb.Append("\"sale_kdlxr\":\"" + dtResult.Rows[i]["sale_kdlxr"].ToString() + "\",");
            sb.Append("\"sale_kdtel\":\"" + dtResult.Rows[i]["sale_kdtel"].ToString() + "\",");
            sb.Append("\"zt\":\"" + dtResult.Rows[i]["zt"].ToString() + "\",");
            sb.Append("\"sl\":\"" + dtResult.Rows[i]["sl"].ToString() + "\",");
            sb.Append("\"requesttime\":\"" + dtResult.Rows[i]["requesttime"].ToString() + "\",");
            sb.Append("\"createtime\":\"" + dtResult.Rows[i]["createtime"].ToString() + "\"}");
            if (i < dtResult.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //查询快递类型下拉选项
    public void getkdgs(HttpContext context)
    {
        string sql = @"select id,typename from t_type where typelx='快递公司' and isactive='Y' order by xh ";
        DataTable dt = DBHelper.ExecuteDataTable(sql);
        string fsmessage = "";
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        sb.Append("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"typename\":\"" + dt.Rows[i]["typename"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }

    public class GetDate
    {
        public List<main> ArrayObj_main { get; set; }//主表信息
        public List<mx> ArrayObj_mx { get; set; }//明细信息
    }
    public class main
    {
        //单据编号
        public string id;
        public string ID
        {
            get { return id; }
            set { id = value; }
        }
        //单据编号
        public string docno;
        public string DOCNO
        {
            get { return docno; }
            set { docno = value; }
        }
        //店仓编号
        public string userid;
        public string USERID
        {
            get { return userid; }
            set { userid = value; }
        }
        //店仓编号
        public string customerCode;
        public string CUSTOMERCODE
        {
            get { return customerCode; }
            set { customerCode = value; }
        }
        //收货人姓名
        public string shr;
        public string SHR
        {
            get { return shr; }
            set { shr = value; }
        }
        //手机号
        public string phone;
        public string PHONE
        {
            get { return phone; }
            set { phone = value; }
        }
        //企微手机号
        public string mobile;
        public string MOBILE
        {
            get { return mobile; }
            set { mobile = value; }
        }
        //收货人地址
        public string address;
        public string ADDRESS
        {
            get { return address; }
            set { address = value; }
        }
        //备注
        public string remark;
        public string REMARK
        {
            get { return remark; }
            set { remark = value; }
        }


        //快递公司
        public string txt_kdgs;
        public string TXT_KDGS
        {
            get { return txt_kdgs; }
            set { txt_kdgs = value; }
        }
        //快递单号
        public string txt_kddh;
        public string TXT_KDDH
        {
            get { return txt_kddh; }
            set { txt_kddh = value; }
        }
        //发货人
        public string txt_kdfhr;
        public string TXT_KDFHR
        {
            get { return txt_kdfhr; }
            set { txt_kdfhr = value; }
        }
        //发货人联系电话
        public string txt_kdtel;
        public string TXT_KDTEL
        {
            get { return txt_kdtel; }
            set { txt_kdtel = value; }
        }

    }
    //尺码
    public class mx
    {
        //tmid
        public string tmid;
        public string TMID
        {
            get { return tmid; }
            set { tmid = value; }
        }

        //数量
        public int? sl;
        public int? SL
        {
            get { return sl; }
            set { sl = value; }
        }
        //备注
        public string mark;
        public string MARK
        {
            get { return mark; }
            set { mark = value; }
        }

        public string imageurl;
        public string IMAGEURL
        {
            get { return imageurl; }
            set { imageurl = value; }
        }

    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}